package com.zhiche.lisa.bms.service.price.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.price.PricePurchaseDtlMapper;
import com.zhiche.lisa.bms.dao.model.price.PricePurchaseDtl;
import com.zhiche.lisa.bms.service.price.PricePurchaseDtlService;
import com.zhiche.lisa.core.supports.BaseException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 支付价格明细 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-10-31
 */
@Service
public class PricePurchaseDtlServiceImpl extends ServiceImpl<PricePurchaseDtlMapper, PricePurchaseDtl> implements PricePurchaseDtlService {

    /**
     * 根据绑定头id和列信息进行新增
     *
     * @param pricePurchaseDtls
     * @param headerId
     * @return
     */
    @Override
    public List<PricePurchaseDtl> insertPricePurchaseDtls(List<PricePurchaseDtl> pricePurchaseDtls, Long headerId) {
        try {
            if (CollectionUtils.isEmpty(pricePurchaseDtls)) {
                return null;
            }
            pricePurchaseDtls.forEach(pricePurchaseDtl -> {
                pricePurchaseDtl.setHeadId(headerId);
            });
            insertBatch(pricePurchaseDtls);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("新增支付价格明细失败");
        }
        return pricePurchaseDtls;
    }

    @Override
    public PricePurchaseDtl insertPricePurchaseDtl(PricePurchaseDtl pricePurchaseDtl, Long headerId) {
        try {
            if (pricePurchaseDtl == null) return null;
            pricePurchaseDtl.setHeadId(headerId);
            insert(pricePurchaseDtl);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            throw new BaseException("新增支付价格明细失败");
        }
        return pricePurchaseDtl;
    }

    @Override
    public List<PricePurchaseDtl> selectPricePurchaseDtlByHeadId(Long headerId) {
        EntityWrapper<PricePurchaseDtl> pricePurchaseDtlEntityWrapper = new EntityWrapper<>();
        pricePurchaseDtlEntityWrapper.eq("head_id", headerId);
        return baseMapper.selectListNoTenant(pricePurchaseDtlEntityWrapper);
    }

    /**
     * 根据指令号获取数量, 判断是否存在
     *
     * @param orderCode
     * @return
     */
    @Override
    public int getCountByOrderCode(String orderCode) {
        if (!StringUtils.isEmpty(orderCode)) {
            return baseMapper.countVerifyHuiyuncheNoTenant(orderCode);
        }
        return 0;
    }
}
